#include "CheckUpdata.h"
#include <iostream>
#include "AuClient.h"
#include "Request.h"
#include "Response.h"
#include "JsonUtil.h"
#include "FileUtil.h"
#include "UpdataLogic.h"
#include "ConfigUtil.h"
using namespace std;

CheckUpdata::CheckUpdata(/* args */)
{
}

CheckUpdata::~CheckUpdata()
{
}

bool CheckUpdata::execute()
{
    map<string,string> paramMap=ConfigUtil::paraConfig("./config/updata.ini","config");
    paramMap["type"]="0";

    //实例化一个Request请求对象
    Request request("192.168.38.128",8080,paramMap);
    AuClient client;

    Response *response=client.execute(request);
    if (response->getSuccess())
    {
        string data=response->getData();
        map<string,string> resMap=JsonUtil::strjsonParseMap(data);
        string isUpdata=resMap["isUpdata"];
        if (isUpdata=="0")
        {
            cout<<response->getMessage()<<endl;
            return false;
        }
        UpdataLogic logic;
        string policy=resMap["policy"];
        resMap["installPath"]=paramMap["installPath"];
        if (policy=="auto")
        {
            cout<<"===开始强制更新==="<<endl;
            logic.execute(resMap);
        }else
        {
            int NewUpadta=1;
            cout<<"请选择是否进行更新->1:立即更新  2:稍后更新"<<endl;
            cin>>NewUpadta;
            if (NewUpadta==1)
            {
                cout<<"===立即更新==="<<endl;
                logic.execute(resMap);
            }else
            {
                cout<<"===稍后更新==="<<endl;
            }
        }
    }else
    {
        cout<<"===更新检测失败==="<<endl;
    }
    
    delete response;
    return false;
}